﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;//Please add references

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:AccountingAttendance
    /// </summary>
    public partial class AccountingAttendance
    {
        public AccountingAttendance()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "AccountingAttendance");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from AccountingAttendance");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.AccountingAttendance model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into AccountingAttendance(");
            strSql.Append("StaffID,AttendanceMonth,InsertDate,ContractID,LegalDays,FactDays,NormalAddTimes,WeekEndAddTimes,HolidayAddTimes)");
            strSql.Append(" values (");
            strSql.Append("@StaffID,@AttendanceMonth,@InsertDate,@ContractID,@LegalDays,@FactDays,@NormalAddTimes,@WeekEndAddTimes,@HolidayAddTimes)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@AttendanceMonth", DbType.String),
                    new SQLiteParameter("@InsertDate", DbType.String),
                    new SQLiteParameter("@ContractID", DbType.Int32,8),
                    new SQLiteParameter("@LegalDays", DbType.String),
                    new SQLiteParameter("@FactDays", DbType.String),
                    new SQLiteParameter("@NormalAddTimes", DbType.Decimal,4),
                    new SQLiteParameter("@WeekEndAddTimes", DbType.Decimal,4),
                    new SQLiteParameter("@HolidayAddTimes", DbType.Decimal,4)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.AttendanceMonth;
            parameters[2].Value = model.InsertDate;
            parameters[3].Value = model.ContractID;
            parameters[4].Value = model.LegalDays;
            parameters[5].Value = model.FactDays;
            parameters[6].Value = model.NormalAddTimes;
            parameters[7].Value = model.WeekEndAddTimes;
            parameters[8].Value = model.HolidayAddTimes;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.AccountingAttendance model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update AccountingAttendance set ");
            strSql.Append("StaffID=@StaffID,");
            strSql.Append("AttendanceMonth=@AttendanceMonth,");
            strSql.Append("InsertDate=@InsertDate,");
            strSql.Append("ContractID=@ContractID,");
            strSql.Append("LegalDays=@LegalDays,");
            strSql.Append("FactDays=@FactDays,");
            strSql.Append("NormalAddTimes=@NormalAddTimes,");
            strSql.Append("WeekEndAddTimes=@WeekEndAddTimes,");
            strSql.Append("HolidayAddTimes=@HolidayAddTimes");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@AttendanceMonth", DbType.String),
                    new SQLiteParameter("@InsertDate", DbType.String),
                    new SQLiteParameter("@ContractID", DbType.Int32,8),
                    new SQLiteParameter("@LegalDays", DbType.String),
                    new SQLiteParameter("@FactDays", DbType.String),
                    new SQLiteParameter("@NormalAddTimes", DbType.Decimal,4),
                    new SQLiteParameter("@WeekEndAddTimes", DbType.Decimal,4),
                    new SQLiteParameter("@HolidayAddTimes", DbType.Decimal,4),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.AttendanceMonth;
            parameters[2].Value = model.InsertDate;
            parameters[3].Value = model.ContractID;
            parameters[4].Value = model.LegalDays;
            parameters[5].Value = model.FactDays;
            parameters[6].Value = model.NormalAddTimes;
            parameters[7].Value = model.WeekEndAddTimes;
            parameters[8].Value = model.HolidayAddTimes;
            parameters[9].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AccountingAttendance ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AccountingAttendance ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.AccountingAttendance GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AttendanceMonth,InsertDate,ContractID,LegalDays,FactDays,NormalAddTimes,WeekEndAddTimes,HolidayAddTimes from AccountingAttendance ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.AccountingAttendance model = new Accounting.Model.AccountingAttendance();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AttendanceMonth"] != null && ds.Tables[0].Rows[0]["AttendanceMonth"].ToString() != "")
                {
                    model.AttendanceMonth = ds.Tables[0].Rows[0]["AttendanceMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["InsertDate"] != null && ds.Tables[0].Rows[0]["InsertDate"].ToString() != "")
                {
                    model.InsertDate = ds.Tables[0].Rows[0]["InsertDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ContractID"] != null && ds.Tables[0].Rows[0]["ContractID"].ToString() != "")
                {
                    model.ContractID = int.Parse(ds.Tables[0].Rows[0]["ContractID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LegalDays"] != null && ds.Tables[0].Rows[0]["LegalDays"].ToString() != "")
                {
                    model.LegalDays = ds.Tables[0].Rows[0]["LegalDays"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FactDays"] != null && ds.Tables[0].Rows[0]["FactDays"].ToString() != "")
                {
                    model.FactDays = ds.Tables[0].Rows[0]["FactDays"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NormalAddTimes"] != null && ds.Tables[0].Rows[0]["NormalAddTimes"].ToString() != "")
                {
                    model.NormalAddTimes = decimal.Parse(ds.Tables[0].Rows[0]["NormalAddTimes"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WeekEndAddTimes"] != null && ds.Tables[0].Rows[0]["WeekEndAddTimes"].ToString() != "")
                {
                    model.WeekEndAddTimes = decimal.Parse(ds.Tables[0].Rows[0]["WeekEndAddTimes"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HolidayAddTimes"] != null && ds.Tables[0].Rows[0]["HolidayAddTimes"].ToString() != "")
                {
                    model.HolidayAddTimes = decimal.Parse(ds.Tables[0].Rows[0]["HolidayAddTimes"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AttendanceMonth,InsertDate,ContractID,LegalDays,FactDays,NormalAddTimes,WeekEndAddTimes,HolidayAddTimes ");
            strSql.Append(" FROM AccountingAttendance ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "Attendance";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal int getOtherContract(string p, string staffID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  sum(FactDays) as sumFactDay from AccountingAttendance ");
            strSql.Append(" where AttendanceMonth=@AttendanceMonth");
            strSql.Append(" and StaffID=@StaffID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@AttendanceMonth", DbType.String),
                    new SQLiteParameter("@StaffID", DbType.Int32),
};
            parameters[0].Value = p;
            parameters[1].Value = staffID;

            Accounting.Model.AccountingAttendance model = new Accounting.Model.AccountingAttendance();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);

            try
            {
                return int.Parse(ds.Tables[0].Rows[0]["sumFactDay"].ToString());
            }
            catch
            {
                return 0;
            }
        }

        internal DataSet GetListWithStaffID(string staffID, string p)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ContractID from AccountingAttendance ");
            strSql.Append(" where AttendanceMonth=@AttendanceMonth");
            strSql.Append(" and StaffID=@StaffID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@AttendanceMonth", DbType.String),
                    new SQLiteParameter("@StaffID", DbType.Int32),
};
            parameters[0].Value = p;
            parameters[1].Value = staffID;

            Accounting.Model.AccountingAttendance model = new Accounting.Model.AccountingAttendance();
            return DbHelperSQLite.Query(strSql.ToString(), parameters);
        }

        internal Model.AccountingAttendance GetModel(int contractID, int staffID, string calcMonth)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AttendanceMonth,InsertDate,ContractID,LegalDays,FactDays,NormalAddTimes,WeekEndAddTimes,HolidayAddTimes from AccountingAttendance ");
            strSql.Append(" where AttendanceMonth=@AttendanceMonth");
            strSql.Append(" and ContractID=@ContractID");
            strSql.Append(" and StaffID=@StaffID");
            SQLiteParameter[] parameters = {
                                               new SQLiteParameter("@AttendanceMonth", DbType.String),
                                               new SQLiteParameter("@ContractID", DbType.Int32,4),
                    new SQLiteParameter("@StaffID", DbType.Int32,4)
};
            parameters[0].Value = calcMonth;
            parameters[1].Value = contractID;
            parameters[2].Value = staffID;

            Accounting.Model.AccountingAttendance model = new Accounting.Model.AccountingAttendance();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AttendanceMonth"] != null && ds.Tables[0].Rows[0]["AttendanceMonth"].ToString() != "")
                {
                    model.AttendanceMonth = ds.Tables[0].Rows[0]["AttendanceMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["InsertDate"] != null && ds.Tables[0].Rows[0]["InsertDate"].ToString() != "")
                {
                    model.InsertDate = ds.Tables[0].Rows[0]["InsertDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ContractID"] != null && ds.Tables[0].Rows[0]["ContractID"].ToString() != "")
                {
                    model.ContractID = int.Parse(ds.Tables[0].Rows[0]["ContractID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LegalDays"] != null && ds.Tables[0].Rows[0]["LegalDays"].ToString() != "")
                {
                    model.LegalDays = ds.Tables[0].Rows[0]["LegalDays"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FactDays"] != null && ds.Tables[0].Rows[0]["FactDays"].ToString() != "")
                {
                    model.FactDays = ds.Tables[0].Rows[0]["FactDays"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NormalAddTimes"] != null && ds.Tables[0].Rows[0]["NormalAddTimes"].ToString() != "")
                {
                    model.NormalAddTimes = decimal.Parse(ds.Tables[0].Rows[0]["NormalAddTimes"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WeekEndAddTimes"] != null && ds.Tables[0].Rows[0]["WeekEndAddTimes"].ToString() != "")
                {
                    model.WeekEndAddTimes = decimal.Parse(ds.Tables[0].Rows[0]["WeekEndAddTimes"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HolidayAddTimes"] != null && ds.Tables[0].Rows[0]["HolidayAddTimes"].ToString() != "")
                {
                    model.HolidayAddTimes = decimal.Parse(ds.Tables[0].Rows[0]["HolidayAddTimes"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}